<!--
  ~ DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  ~
  ~ Copyright (c) 2008-2011, Salzburg Research. All rights reserved.
  ~
  ~ Redistribution and use in source and binary forms, with or without
  ~ modification, are permitted provided that the following conditions are met:
  ~ - Redistributions of source code must retain the above copyright notice,
  ~ this list of conditions and the following disclaimer.
  ~ - Redistributions in binary form must reproduce the above copyright notice,
  ~ this list of conditions and the following disclaimer in the documentation
  ~ and/or other materials provided with the distribution.
  ~ - Neither the name of the KiWi Project nor the names of its contributors
  ~ may be used to endorse or promote products derived from this software
  ~ without specific prior written permission.
  ~
  ~ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  ~ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  ~ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ~ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  ~ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  ~ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  ~ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  ~ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  ~ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ~ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  ~ POSSIBILITY OF SUCH DAMAGE.
  ~
  ~ Contributor(s):
  ~ tkurz
  ~
  ~ File Info:
  ~ User: tkurz
  ~ Date: 2011/01/28
  ~ Time: 10:23
  ~
  ~ Project: KiWi2, KiWi2 module
  -->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>file upload tester</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
    <script type="text/javascript">

        var host = "http://localhost:8080/"
        var app = "KiWi2";

        $(document).ready(function() {
            $("#url").val(host+app+"/resource/1234");
        });

        function getResourceUUID(url) {
            var cutSize = (host+app+"/resource/").length;
            return url.substring(cutSize);
        }

        function uploadText() {
            var text = $("#text_content").val();
            var mime = $("#text_mime option:selected").val();
            upload(mime,text);
        }

        var file_mime;

        function uploadFile() {
            var file = document.getElementById('file_content').files[0];
            file_mime = file.type;
            getAsText(file);
        }

        function upload(mime,data) {
            var url = $("#url").val();
            if(url == "") {
                alert("please insert resource url");
                return;
            }
            var uuid = getResourceUUID(url);
            url = host+app+"/content/"+mime+"/"+uuid;

           /* $.ajax({
                type:"PUT",
                contentType: mime,
                url: url,
                data: data,
                success: function(result) {
                    if (confirm("Loaded successful! Redirect to resource content?")) {
                        window.location.replace(url);
                    }
                },
                error: function(jXHR) {
                    alert("Error: "+jXHR.statusText);
                }
            });*/
            var xhr = new XMLHttpRequest;

            xhr.open("PUT", url, true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if(xhr.status == 200) {
                        if (confirm("Loaded successful! Redirect to resource content?")) {
                            window.location.replace(url);
                        }
                    } else {
                        alert("(Status "+xhr.status+") "+xhr.responseText);
                    }

                }
            };
            var rel = $("#rel").val();
            var contentType = mime+";rel="+rel;
            xhr.setRequestHeader("Content-Type", contentType);

            // finally send the request as binary data
            xhr.sendAsBinary(data);
        }

        function getAsText(readFile) {

          var reader = new FileReader();

          // Read file into memory as UTF-16
          reader.readAsBinaryString(readFile);

          // Handle progress, success, and errors
          reader.onload = loaded;
          reader.onerror = errorHandler;
        }

        function loaded(evt) {
          // Obtain the read file data
          var data = evt.target.result;
          upload(file_mime,data)
        }

        function errorHandler(evt) {
          if(evt.target.error.code == evt.target.error.NOT_READABLE_ERR) {
            // The file could not be read
            alert("Error while reading file");
          }
        }


    </script>
</head>
<body>
<form>
    <fieldset>
    <legend>This is a test client for data upload</legend>
    <dl>
      <dt>Resource URL</dt>
      <dd><input type="text" id="url" size="60" value="test"><br>
          <select id="rel">
              <option>meta</option>
              <option>content</option>
          </select>
      </dd>
    </dl>
    <hr>
    <dl>
      <dt>Text Upload</dt>
      <dd>
          <textarea cols="50" rows="10" id="text_content"></textarea><br>
          MIME Type:
          <select id="text_mime">
              <option>text/html</option>
              <option>text/plain</option>
          </select><br>
          <input type="button" value="submit" onclick="uploadText()">
      </dd>
      <hr>
      <dt>File Upload</dt>
      <dd>
          Datei:<input type="file" id="file_content"><div id="progress"></div><br>
          <!--Name:<input type="text" id="file_title"><br>-->
          <input type="button" value="submit" onclick="uploadFile()">
      </dd>
    </dl>
    </fieldset>
</form>
</body>
</html>